package queue.practise;

import java.util.Stack;

/**
 * 通过两个栈来实现队列
 * stack1是用来存储的
 * stack2是用来输出的
 */
public class MyQueue {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    public MyQueue() {
        this.stack1 = new Stack<>();
        this.stack2 = new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    public static void remove(Stack<Integer> dest, Stack<Integer> src) {
        int size = src.size();
        for (int i = 0; i < size; i++) {
            dest.add(src.pop());
        }
    }
    private static boolean checkRemove(Stack<Integer> dest, Stack<Integer> src) {
        if(dest.isEmpty()) {
            if(src.empty()) {
                return false;
            }
            remove(dest, src);
        }
        return true;
    }
    public int pop() {
        if(!checkRemove(stack2, stack1)) {
            return -1;
        }
        return stack2.pop();
    }
    
    public int peek() {
        if(!checkRemove(stack2, stack1)) {
            return -1;
        }
        return stack2.peek();
    }
    public boolean empty() {
        return stack2.isEmpty() && stack1.isEmpty();
    }
}
